package neusoft.yuejian_11.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import neusoft.yuejian_11.base.BaseActivity;
import neusoft.yuejian_11.bean.AttentionBean;
import neusoft.yuejian_11.bean.ClubBean;
import neusoft.yuejian_11.bean.IsAttentionBean;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.model.AttentionModel;
import neusoft.yuejian_11.model.ClubModel;
import neusoft.yuejian_11.R;

public class ClubDetailActivity extends BaseActivity implements RetrofitListener{
    protected Context context;
    private int clubId;
    private String clubName;
    private String clubPic;
    private TextView textClubName;
    private ImageButton imageButtonFavor;
    private ImageButton imageButtonBack;
    private ImageView imageView;
    private Button buttonCourse;
    private Button buttonVideo;
    private TextView textClubInfo;
    private TextView textClubTime;
    private TextView textClubAddress;
    private TextView textxClubPhone;
    private TextView textClubIntroduce;
    private Boolean flags;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_detail);

        initViews();//初始化
        initEvents();//点击事件处理
        showData();//传参
        initData();//网络获取
    }

    private void initViews() {
        imageView = (ImageView) findViewById(R.id.imageView);
        textClubName = (TextView) findViewById(R.id.textView_club_name);

        textClubInfo = (TextView) findViewById(R.id.text_club_info);
        textClubAddress = (TextView) findViewById(R.id.text_club_address);
        textxClubPhone = (TextView) findViewById(R.id.text_club_phone);
        textClubTime = (TextView) findViewById(R.id.text_club_time);
        textClubIntroduce = (TextView) findViewById(R.id.text_club_introduce);

        imageButtonFavor = (ImageButton) findViewById(R.id.imageButtom_favor);
        imageButtonBack = (ImageButton) findViewById(R.id.imageButtom_back);

        buttonCourse = (Button) findViewById(R.id.button_course);
        buttonVideo = (Button) findViewById(R.id.button_video);
    }

    private void initEvents() {
        buttonCourse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ClubDetailActivity.this, CourseListActivity.class);
                intent.putExtra("c_id", clubId);
                intent.putExtra("c_name", clubName);
                intent.putExtra("c_pic", clubPic);
                startActivity(intent);
            }
        });

        buttonVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ClubDetailActivity.this, VideoListActivity.class);
                intent.putExtra("c_id", clubId);
                intent.putExtra("c_name", clubName);
                intent.putExtra("c_pic", clubPic);
                startActivity(intent);
            }
        });

        imageButtonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        imageButtonFavor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClubModel clubModel = new ClubModel();
                clubModel.favorClub(getU_id(),String.valueOf(clubId),ClubDetailActivity.this);
            }
        });
    }

    private void showData() {
        clubId = getIntent().getIntExtra("c_id",-1);
        clubName = getIntent().getStringExtra("c_name");
        textClubName.setText(clubName);
        clubPic = getIntent().getStringExtra("c_pic");
        Picasso.with(context)
                .load(clubPic)
                .into(imageView);
    }

    private void initData() {
        ClubModel clubModel = new ClubModel();
        clubModel.clubDetail(String.valueOf(clubId),this);

        AttentionModel attentionModel = new AttentionModel();
        attentionModel.isCollectedClub(String.valueOf(getU_id()),String.valueOf(clubId),"4",this);
    }


    @Override
    public void onSuccess(Object o, int flag) {
        if (flag==1) {
            ClubBean clubBeanDetail = (ClubBean) o;
            textClubInfo.setText(clubBeanDetail.getC_info());
            textClubAddress.setText(clubBeanDetail.getC_address());
            textxClubPhone.setText(clubBeanDetail.getC_phone());
            textClubTime.setText(clubBeanDetail.getC_time());
            textClubIntroduce.setText(clubBeanDetail.getC_introduce());
        }
        if (flag==3){
            IsAttentionBean isAttentionBean = (IsAttentionBean) o;
            if (isAttentionBean.getCollected().equals("1")) {
                //已关注
                flags = true;
                imageButtonFavor.setImageResource(R.drawable.ic_alikepost);
            } else {
                //未关注的
                flags = false;
                imageButtonFavor.setImageResource(R.drawable.ic_likepost);
            }
        }
        if (flag==2) {
            AttentionBean attentionBean=(AttentionBean) o;
            if (attentionBean.getSuccess().equals("1")) {
                Log.e(attentionBean.getSuccess(), "---------");
                if (flags) {
                    flags = false;
                    imageButtonFavor.setImageResource(R.drawable.ic_likepost);
                } else {
                    flags = true;
                    imageButtonFavor.setImageResource(R.drawable.ic_alikepost);

                }
                Toast.makeText(ClubDetailActivity.this, "操作成功", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onFail() {

    }
}
